﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using System.IO;
using Microsoft.Xna.Framework.Graphics;

namespace KduneeShaders.Content
{
    /// <summary>
    /// Procesor modeli, uwzględniający Normal, Tangent i Binormal - wykorzystywane w bump mappingu.
    /// </summary>
    [ContentProcessor]
    class NormalMappedModelProcessor : ModelProcessor
    {
        /// <summary>
        /// Przetwórz węzeł danych.
        /// </summary>
        /// <param name="input">Węzeł danych</param>
        /// <param name="context">Kontekst przetwarzania</param>
        /// <returns>Zawartość modelu</returns>
        public override ModelContent Process(NodeContent input, ContentProcessorContext context)
        {
            CalculateNormals(input);
            return base.Process(input, context);
        }

        void CalculateNormals(NodeContent node)
        {
            MeshContent mesh = node as MeshContent;

            if (mesh != null)
            {
                MeshHelper.CalculateNormals(mesh, false); // true
                MeshHelper.CalculateTangentFrames(mesh, VertexChannelNames.TextureCoordinate(0),
                                                        VertexChannelNames.Tangent(0),
                                                        VertexChannelNames.Binormal(0));
            }

            foreach (NodeContent child in node.Children)
            {
                CalculateNormals(child);
            }
        }
    }
}
